/**
 * @class ImportSelector
 */
var ImportSelector = Class.create( {

    initialize: function() {
        if (editor.isReadOnlyMode()) return;

        var _this = this;

        /*var mainDiv = new Element('div', {'class': 'import-selector'});

        var promptImport = new Element('div', {'class': 'import-section'}).update("数据加载成功");
        mainDiv.insert(promptImport).insert(this.importValue);
        var buttons = new Element('div', {'class' : 'buttons import-block-bottom'});
        buttons.insert(new Element('input', {type: 'button', name : 'import', 'value': '开始画图', 'class' : 'button', 'id': 'import_button'}).wrap('span', {'class' : 'buttonwrapper'}));
        buttons.insert(new Element('input', {type: 'button', name : 'cancel', 'value': '取消加载', 'class' : 'button secondary'}).wrap('span', {'class' : 'buttonwrapper'}));
        mainDiv.insert(buttons);

        var cancelButton = buttons.down('input[name="cancel"]');
        cancelButton.observe('click', function(event) {
            _this.hide();
        })
        var importButton = buttons.down('input[name="import"]');
        importButton.observe('click', function(event) {
            _this._onImportStarted();
        })

        var closeShortcut = ['Esc'];
        this.dialog = new PhenoTips.widgets.ModalPopup(mainDiv, {close: {method : this.hide.bind(this), keys : closeShortcut}}, {extraClassName: "pedigree-import-chooser", title: "家树医疗", displayCloseButton: true});*/
        //setTimeout(function () {
        //if(document.readyState=="complete")
           // _this._onImportStarted();
        //},3000);
        var interval=window.setInterval(function () {
            if(document.readyState=="complete") {
                _this._onImportStarted();
                window.clearInterval(interval);
            }
        },100);

    },
    handleFileUpload: function(files) {
        for (var i = 0, numFiles = files.length; i < numFiles; i++) {
            var nextFile = files[i];
            console.log("loading file: " + nextFile.name + ", size: " + nextFile.size);

            var _this = this;
            var fr = new FileReader();
            fr.onload = function(e) {
                _this.importValue.value = e.target.result;  
            };
            fr.readAsText(nextFile);
        }
    },

    disableEnableOptions: function() {
        var importType = $$('input:checked[type=radio][name="select-type"]')[0].value;
        var pedOnlyOptions = $$('input[type=radio][name="select-options"]');
        for (var i = 0; i < pedOnlyOptions.length; i++) {
            if (importType != "ped") {
                pedOnlyOptions[i].disabled = true;
            } else {
                pedOnlyOptions[i].disabled = false;
            }
        }
        var pedAndGedcomOption = $$('input[type=checkbox][name="mark-evaluated"]')[0];
        if (importType != "ped" && importType != "gedcom") {
            pedAndGedcomOption.disabled = true;
        } else {
            pedAndGedcomOption.disabled = false;
        }

        var saveExternalID = $$('input[type=checkbox][name="mark-external"]')[0];
        if (importType == "simpleJSON") {
            saveExternalID.disabled = true;
        } else {
            saveExternalID.disabled = false;
        }
    },

    /**
     *
     * @param event
     * @param pictureBox
     * @private
     */
    _onImportStarted: function() {        
        //var _familyJSONData = store.get('familyJSONData');
        //var importValue = _familyJSONData;
       // console.log("Importing:\n" + importValue);
        // this.hide();
        // if (!importValue || importValue == "") {
        //     alert("导入数据为空!");
        //     return;
        // }

        // var param = {
        //     userId: String(getCookie("userid")),
        //     productId: "1",
        //     pageIndex: 1,
        //     pageSize: 5
        // };
        // jQuery.ajax({
        //     url:"http://14.18.33.32:8082/2.0/home/getHome",
        //     type:"POST",
        //     dataType: "json",
        //     data:JSON.stringify(param),
        //     contentType: "application/json",
        //     headers : {'Authorization':JSON.parse(sessionStorage.getItem("vuex")).token},
        //     success:function(data){
        //         console.log(data);
        //     },
        // })
        var drawState = parent.$("#editType").val();
        if(drawState == "add"){
            var importType = "simpleJSON";
            var importOptions = {};
            // var importValue = sessionStorage.getItem("familyJSONData");
            // var probandData = sessionStorage.getItem("probandData");
            // var pedigreeData = sessionStorage.getItem("pedigreeData");

            var importValue = parent.$("#familyJSONData").val();
            //var probandData = parent.$("#probandData").val();
            //var pedigreeData = parent.$("#pedigreeData").val();
            editor.getSaveLoadEngine().createGraphFromImportData(importValue, importType, importOptions,false , true);
            window.setTimeout(function () {
                editor.getWorkspace().centerAroundNode(0);
            },1000);
            jQuery("#viewriskreport_button").attr("disabled","disabled");
            jQuery("#viewriskreport_button").attr("title","请先保存再进行遗传风险评估");
        }else if(drawState == "edit"){
            var importType = "simpleJSON";
            var importOptions = {};
            // var importValue = sessionStorage.getItem("familyJSONData");
            // var probandData = sessionStorage.getItem("probandData");
            // var pedigreeData = sessionStorage.getItem("pedigreeData");
            var importValue = parent.$("#familyJSONData").val();
            //var probandData = parent.$("#probandData").val();
            // var pedigreeData = parent.$("#pedigreeData").val();
            editor.getSaveLoadEngine().createGraphFromImportData(importValue, importType, importOptions,false , true);
            window.setTimeout(function () {
                editor.getWorkspace().centerAroundNode(0);
            },1000);
            jQuery("#viewriskreport_button").removeAttr("disabled");
            jQuery("#viewriskreport_button").removeAttr("title");
            editor.getActionStack().setSaveData(); 
        }
    },

    /**
     *
     * @method show
     */
    show: function() {
        //this.dialog.show();
    },

    /**
     *
     * @method hide
     */
    hide: function() {
        //this.dialog.closeDialog();
    }
});